package com.seashop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seashop.entity.UserTransferBank;
import com.seashop.request.UserTransferBankRequestParam;
import com.seashop.response.ResponseBase;
import com.seashop.service.IUserTransferBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 用户转账银行 前端控制器
 * </p>
 *
 * @author moredln
 * @since 2020-12-02
 */
@RestController
@Api(tags = "银行卡管理")
@Slf4j
@Validated
@RequestMapping("/bank")
public class UserTransferBankController {

    @Autowired
    private IUserTransferBankService userTransferBankService;

    @GetMapping("/listUserTransferBank")
    @ApiOperation(value = "获取用户个人银行卡")
    @ApiImplicitParam(value = "用户ID", name = "userId", dataType = "integer", required = true)
    public ResponseBase<List<UserTransferBank>> listUserTransferBank(@NotBlank Integer userId) {
        List<UserTransferBank> userTransferBanks = userTransferBankService.list(new QueryWrapper<UserTransferBank>().eq("user_id", userId).eq("is_delete", 0).orderByDesc("is_default"));
        return ResponseBase.success(userTransferBanks);
    }


    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "保存或更新个人银行卡")
    public ResponseBase saveOrUpdate(@RequestBody UserTransferBankRequestParam param) {
        UserTransferBank userTransferBank = new UserTransferBank();
        userTransferBankService.saveOrUpdate(userTransferBank);
        return ResponseBase.success();
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除个人银行卡")
    public ResponseBase delete(@NotBlank Integer id) {
        UserTransferBank userTransferBank = userTransferBankService.getById(id);
        if (null == userTransferBank) {
            return ResponseBase.fail("bank not exsit");
        }
        userTransferBank.setIsDelete(1);
        userTransferBankService.updateById(userTransferBank);
        return ResponseBase.success();
    }

}

